<template>
    <el-dialog v-model="visible" :title="title" :width="width" @close="close" @open="open" :close-on-press-escape="false">
        <el-form :inline="true"
                 ref="formRef"
                 :model="obj"
                 :rules="rules"
                 label-position="left" label-width="100px" label-suffix="："
        >
        </el-form>
        <template #footer>
            <el-button @click="visible = false">取消</el-button>
            <template v-if="type===0">
                <el-button type="primary" @click="submitAdd">确认添加</el-button>
            </template>
            <template v-else>
                <el-button type="primary" @click="submitUpdate">保存变更</el-button>
            </template>
        </template>
    </el-dialog>
</template>

<script>
import {getCurrentInstance, toRefs} from "vue";
import {ElLoading, ElMessage} from "element-plus";
import {ref,reactive} from "vue";

export default {
    name: "FormTemplate",
    props:['width'],
    emits: ['refresh-data'],
    setup(props,{emit}){
        const globalProperties = getCurrentInstance()?.appContext.config.globalProperties;
        const visible =  ref(false);
        const formRef = ref(null);
        const form = reactive({
            obj:{
                id: null,
                name: null
            },
            rules:{
                roleName: [
                    { required: true, message: '请输入角色编号', trigger: 'blur' },
                    { validator: validateRoleCode,trigger: 'blur'},
                ],
                roleNameCh: [
                    { required: true, message: '请输入角色名称', trigger: 'blur' },
                    { validator: validateRoleNameCh,trigger: 'blur'},
                ],
                sort: [
                    { required: true, message: '请选择排序', trigger: 'blur' }
                ],
                enabled: [
                    { required: true, message: '前选择是否启用', trigger: 'blur' },
                ]
            },
            submitUpdate(){
                formRef.value.validate((valid) => {
                    if (valid) {
                    }
                })
            },
            setObj({id,name}){//使用结构语法防止赋值对象破坏原对象结构，同时能起到匹配命名参数的作用
                form.obj.id = id;
                form.obj.name = name;
            },
            //提交添加
            submitAdd(){
                formRef.value.validate((valid)=>{
                    if(valid){
                    }
                })
            },
            resetForm(){
                formRef.value.resetFields();
                console.log('销毁后表单数据：',form.obj);
            }
        })
        //提交修改

        return {
            ...toRefs(form),
            formRef,
            visible,
        }
    },
    data(){
        return{
            title: '',
            type: 0,
        }
    },
    methods:{
        init(){
            
        }
    },
    created() {
        this.init();
    }
}
</script>

<style scoped>
*{
    /*border: 1px solid red;*/
}
.item{
    display: flex;
    margin-bottom: 20px;
}
.item-title{
    width: 100px;
    color: #a4a4a4;
    line-height: 30px;
    font-size: 16px
}
.el-tree{
    width: 100%;
    border: 1px solid lightgray;
}
.el-form--inline .el-form-item {
    min-width: 270px;
}
</style>